package com.ipay.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class WxJSUtil {
	
	public static void main(String[] args) {
		
		
		
        // 注意 URL 一定要动态获取，不能 hardcode
		String url ="http://localhost:8080/order/testScanCode";
		url = url.replaceAll("http", "https");
		System.out.println(url);
        /*Map<String, String> ret = sign(url,"123456");
        for (Map.Entry entry : ret.entrySet()) {
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }*/
    };

    /**
     * 微信分享参数处理
     * @param url
     * @param jsapi_ticket
     * @return
     */
    public static Map<String, String> sign(String url,String jsapi_ticket) {
        Map<String, String> ret = new HashMap<String, String>();
        //这里的jsapi_ticket是获取的jsapi_ticket。
        if(jsapi_ticket == null){
        	jsapi_ticket = JsapiTicketUtil.getJSApiTicket();
        }
        //String jsapi_ticket = JsapiTicketUtil.getJSApiTicket();
        //String jsapi_ticket = "HoagFKDcsGMVCIY2vOjf9gFcZ0ALMnqwD3J-_r8Z4ttoU0p4xUlFoyEu1Vc6-Ami3FpUuOMBjOWvnNK99G7Tvg";
        String nonce_str = create_nonce_str();
        String timestamp = create_timestamp();
        String string1;
        String signature = "";
        //注意这里参数名必须全部小写，且必须有序
        string1 = "jsapi_ticket=" + jsapi_ticket +
                  "&noncestr=" + nonce_str +
                  "&timestamp=" + timestamp +
                  "&url=" + url;
       // System.out.println(string1);
        try{
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(string1.getBytes("UTF-8"));
            signature = byteToHex(crypt.digest());
            System.out.println("JSSDK签名："+signature);
        }catch (NoSuchAlgorithmException e){
            e.printStackTrace();
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        ret.put("url", url);
        ret.put("jsapi_ticket", jsapi_ticket);
        ret.put("nonceStr", nonce_str);
        ret.put("timestamp", timestamp);
        ret.put("signature", signature);
        return ret;
    }

    private static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash)
        {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }

    private static String create_nonce_str() {
        return UUID.randomUUID().toString();
    }

    private static String create_timestamp() {
        return Long.toString(System.currentTimeMillis() / 1000);
    }
}
